#! /usr/bin/python
ident = '$Id: forms.py 109 2006-06-05 18:46:37Z trevor $'
'''Defines the base rex objects class. All rex objects are derived from this class to
standerdize the xml serialization process.
Child classes define their own data dictionary with the properties they are interested in.
'''
import common, base, sys
from   	xml.sax 	import saxutils
from 	xml.dom		import minidom

################################
class form( base.object ):
	'''Represents the rex form objects'''
	def __init__( self, account = None, module = None, xmlstring = None ):
		assert account, 'No account passed'
		assert module, 'No module'
		self.data = []
		self.data.append( {'name':'id',			'value':'', 'label':'ID', 		'type':base.ROT_NUMBER,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'name',		'value':'', 'label':'Name',		'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'title',		'value':'', 'label':'Title',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'description','value':'', 'label':'Description','type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'header',		'value':'', 'label':'Header','type':base.ROT_TEXT,'rw':True,'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'footer',		'value':'', 'label':'Footer','type':base.ROT_TEXT,'rw':True,'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'reply_email','value':'', 'label':'Reply Address',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'cc_email',	'value':'', 'label':'CC Address','type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'bcc_email',	'value':'', 'label':'BCC Address','type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'bouncer',	'value':'0','label':'Bouncer',	'type':base.ROT_CHOICE,	'rw':True,	'file_upload':False,	'options':[ ('1','Yes'), ('0','No') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'bouncer_email','value':'', 'label':'Bouncer Address','type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'bouncer_group','value':'', 'label':'Bouncer Group',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'created_on',	'value':'', 'label':'Created On','type':base.ROT_DATE,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.object_name = "form"
		self.surveys = []
		base.object.__init__( self, account, module, xmlstring )

	def fromxml( self, xmlstring ):
		'''Expected DOM example
		<form id='100' name='My form' >
			<survey name='survey1' id='130' status='active' />
			<survey name='survey2' id='140' status='archived' />
		</form>'''
		# Call the base method to handle the attributes, then we parse the surveys
		base.object.fromxml( self, xmlstring )

		# Now parse all the surveys
		xmlstring = common.uni2str( xmlstring )
		try:
			sdom 	= minidom.parseString( xmlstring )
			snode	= sdom.firstChild
			for inode in snode.getElementsByTagName("survey"):
				self.surveys.append( survey( self.account, self, inode.toxml() ) )
		except Exception:
			common.show_error( "Failed to load form survey from xml." )

class survey( base.object ):
	'''Represents the survey class'''
	def __init__( self, account = None, form = None, xmlstring = None ):
		assert account
		assert form
		self.module = form.module
		self.data = []
		self.data.append( {'name':'id',				'value':'',	'label':'ID', 		'type':base.ROT_NUMBER,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'form_id',		'value':'', 'label':'Form ID',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'name',			'value':'', 'label':'Name',		'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'title',			'value':'', 'label':'Title',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'description',	'value':'', 'label':'Description','type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'email_subject',	'value':'', 'label':'Email Subject','type':base.ROT_TEXT,	'rw':True,	'file_upload':False,'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'email_body',		'value':'', 'label':'Email Body','type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'email_html',		'value':'0','label':'Email as HTML','type':base.ROT_CHOICE,	'rw':True,	'file_upload':False,'options':[ ('1','Yes'), ('0','No') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'show_welcome',	'value':'0','label':'Show Front Page','type':base.ROT_CHOICE,'rw':True,	'file_upload':False,'options':[ ('1','Yes'), ('0','No') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'r_comment_field','value':'0','label':'Ask for a Comment','type':base.ROT_CHOICE,'rw':True,	'file_upload':False,'options':[ ('1','Yes'), ('0','No') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'t_send_email',	'value':'0','label':'Send a Thank You Email','type':base.ROT_CHOICE,'rw':True,	'file_upload':False,'options':[ ('1','Yes'), ('0','No') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'header',			'value':'', 'label':'Header',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'intro',			'value':'', 'label':'Front Page Body',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'intro_html',		'value':'0','label':'Front Page as HTML','type':base.ROT_CHOICE,	'rw':True,	'file_upload':False,'options':[ ('1','Yes'), ('0','No') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'footer',			'value':'', 'label':'Footer',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'sensitive',		'value':'0','label':'Sensitive','type':base.ROT_CHOICE,	'rw':True,	'file_upload':False,'options':[ ('1','Yes'), ('0','No') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'r_show_prompt',	'value':'0','label':'Prompt for Referral','type':base.ROT_CHOICE,	'rw':True,	'file_upload':False,'options':[ ('1','Yes'), ('0','No') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'referral_link',	'value':'', 'label':'Referral Link URL',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'r_intro',		'value':'', 'label':'Referral Front Page Body',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'r_intro_html',	'value':'0','label':'Referral Front Page as HTML','type':base.ROT_CHOICE,	'rw':True,	'file_upload':False,'options':[ ('1','Yes'), ('0','No') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'r_email_subject','value':'', 'label':'Referral Email Subject',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'r_email_body',	'value':'', 'label':'Referral Email Body',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'r_email_html',	'value':'0','label':'Referral Email as HTML','type':base.ROT_CHOICE,	'rw':True,	'file_upload':False,'options':[ ('1','Yes'), ('0','No') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'thank_you',		'value':'', 'label':'Thank You Page Body',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'thank_you_html',	'value':'0','label':'Thank You Page as HTML','type':base.ROT_CHOICE,	'rw':True,	'file_upload':False,'options':[ ('1','Yes'), ('0','No') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'f_email_subject','value':'', 'label':'Followup Email Subject',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'f_email_body',	'value':'', 'label':'Followup Email Body',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'f_email_html',	'value':'0','label':'Followup Email as HTML','type':base.ROT_CHOICE,	'rw':True,	'file_upload':False,'options':[ ('1','Yes'), ('0','No') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'t_email_subject','value':'', 'label':'Thank You Email Subject',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'t_email_body',	'value':'', 'label':'Thank You Email Body',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'t_email_html',	'value':'0','label':'Thank You Email as HTML','type':base.ROT_CHOICE,	'rw':True,	'file_upload':False,'options':[ ('1','Yes'), ('0','No') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'a_intro',		'value':'', 'label':'Accept Front Page Body',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'a_intro_html',	'value':'0','label':'Accept Front Page as HTML','type':base.ROT_CHOICE,	'rw':True,	'file_upload':False,'options':[ ('1','Yes'), ('0','No') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'reply_email',	'value':'', 'label':'Reply Address',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'cc_email',		'value':'', 'label':'CC Address',		'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'bcc_email',		'value':'', 'label':'BCC Address',		'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'bouncer',		'value':'0','label':'Bouncer',			'type':base.ROT_CHOICE,	'rw':True,	'file_upload':False,	'options':[ ('1','Yes'), ('0','No') ],	'custom':False, 'dirty':False } )
		self.data.append( {'name':'bouncer_email',	'value':'', 'label':'Bouncer Address','type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'bouncer_group',	'value':'', 'label':'Bouncer Group',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'created_on',		'value':'', 'label':'Created On','type':base.ROT_DATE,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'questions',		'value':'', 'label':'Questions',	'type':base.ROT_QUESTIONS,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'notifiers',		'value':'', 'label':'Notifiers',	'type':base.ROT_NOTIFIERS,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.object_name = "survey"
		self.form = form
		self.questions 	= []
		self.notifiers 	= []
		base.object.__init__( self, account, self.module, xmlstring )

	def fromxml( self, xmlstring ):
		'''Expected DOM example
		<survey id='100' form_id='200' name='My survey' >
			<question caption='survey1' id='130' ... />
			<notifier email_address='survey2' id='140'  ... />
		</survey>'''
		# Call the base method to handle the attributes, then we parse the surveys
		base.object.fromxml( self, xmlstring )
		# Now parse all the questions and notifiers
		self.questions 	= []
		self.notifiers 	= []
		xmlstring = common.uni2str( xmlstring )
		try:
			sdom 	= minidom.parseString( xmlstring )
			snode	= sdom.firstChild
			for inode in snode.getElementsByTagName("question"):
				self.questions.append( question( self.account, self, inode.toxml() ) )
			for inode in snode.getElementsByTagName("notifier"):
				n = notifier( self.account, self, inode.toxml() )
				self.notifiers.append( n )
			del snode
			del sdom
		except Exception:
			common.show_error( "Failed to load form questions/notifiers from xml." )

class question( base.object ):
	'''Represents the question class'''
	def __init__( self, account = None, survey = None, xmlstring = None ):
		assert account
		self.module = survey.module
		self.data = []
		self.data.append( {'name':'id',				'value':'',	'label':'ID', 		'type':base.ROT_NUMBER,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'survey_id',		'value':'', 'label':'Survey ID','type':base.ROT_NUMBER,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'kind',			'value':'', 'label':'Kind',		'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'caption',		'value':'', 'label':'Caption',	'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'options',		'value':'', 'label':'Data',		'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'order_index',	'value':'', 'label':'Order Index','type':base.ROT_TEXT,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.object_name= "question"
		self.survey 	= survey
		self.dirty		= False
		self.old_caption= ""
		self.old_data   = ""
		self.old_kind   = ""
		if survey: self["survey_id"] = survey["id"]
		base.object.__init__( self, account, self.module, xmlstring )

class notifier( base.object ):
	'''Represents the notifier class'''
	def __init__( self, account = None, survey = None, xmlstring = None ):
		assert account
		self.module = survey.module
		self.data = []
		self.data.append( {'name':'id',				'value':'',	'label':'ID', 		'type':base.ROT_NUMBER,	'rw':False,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'survey_id',		'value':'', 'label':'Survey ID','type':base.ROT_NUMBER,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'kind',			'value':'', 'label':'Kind',		'type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'email',			'value':'', 'label':'Email Address','type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'email_subject',	'value':'', 'label':'Email Subject','type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.data.append( {'name':'email_body',		'value':'', 'label':'Email Body','type':base.ROT_TEXT,	'rw':True,	'file_upload':False,	'options':None,	'custom':False, 'dirty':False } )
		self.object_name= "notifier"
		self.survey 	= survey
		self.dirty		= False
		self.old_address= ""
		self.old_subject= ""
		self.old_body   = ""
		if survey is not None: self["survey_id"] = survey["id"]
		base.object.__init__( self, account, self.module, xmlstring )
